﻿using System;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNViewFormatImageWorker
	{
		
		public static void  formatImage(View view)
		{
            //CID 0618 //2010.07.05 //Begin
            if (!PathUtil.isResourceFileEnabled(view.Manifest))
			{
				return;
			}
            //CID 0618 //2010.07.05 //End

			XmlElement root = view.Doc.DocumentElement;

			XmlNodeList backgroundAttrList = root.SelectNodes( "//@background" );
			foreach( XmlAttribute backgroundAttr in backgroundAttrList )
			{
				XmlElement ownerElement = backgroundAttr.OwnerElement;
                //CID 0618 //2010.07.05 //Begin
                string backgroundImage = ResourceFileUtil.getResourceFileUrl(view.Manifest, backgroundAttr.Value);
                //CID 0618 //2010.07.05 //End
				if( ownerElement.Name.ToLower().Equals( "body" ) )
				{
					XmlAttribute styleAttr = ownerElement.GetAttributeNode( "style" );
					if( styleAttr != null )
					{
						styleAttr.Value = styleAttr.Value + ";background-image: url(" + backgroundImage + ");";
					}
					else
					{
						ownerElement.SetAttribute( "style", "background-image: url(" + backgroundImage + ");" );
					}
				}
				else
				{
					backgroundAttr.Value = backgroundImage;
				}
			}

			XmlNodeList srcAttrList = root.SelectNodes( "//@src" );
			foreach( XmlAttribute srcAttr in srcAttrList )
			{
				if( srcAttr.OwnerElement.Name.ToLower().Equals( "img" ) )
				{
                    //CID 0638 //2010.08.07 //res://
                    if (srcAttr.Value.IndexOf("xdImage:getImageUrl") < 0 && srcAttr.Value.IndexOf(view.Manifest.ImageUri.Start) != 0 )
					{
                        //CID 0618 //2010.07.05 //Begin
                        srcAttr.Value = ResourceFileUtil.getResourceFileUrl(view.Manifest, srcAttr.Value);
                        //CID 0618 //2010.07.05 //End
					}
				}
			}
		}
	}
}